package ru.rchervot.sanasar.client.rpc;

import com.google.gwt.user.client.rpc.IsSerializable;

public class GetPage extends Action implements IsSerializable {
	private Integer queryId;
	private Integer offset;
	private Integer pageSize;
	
	public GetPage() {
	}
	
	public GetPage(Integer queryId, Integer offset) {
		this.queryId = queryId;
		this.offset = offset;
	}
	
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	
	public Integer getQueryId() {
		return queryId;
	}
	
	public Integer getOffset() {
		return offset;
	}

	public Integer getPageSize() {
		return pageSize;
	}
	
	@Override
	public boolean equals(Object o) {
		if (o == null) return false;

		if (! (o instanceof GetPage)) return false;

		GetPage to = (GetPage) o;

		return (queryId == null
				? to.getQueryId() == null
						: queryId.equals(to.getQueryId())) 
						&& (this.offset == null
								? to.getOffset() == null
										: getPageNumber().equals(to.getPageNumber()))
										&& (this.pageSize == null
												? to.getPageSize() == null
														: pageSize.equals(to.getPageSize()));
	}

	private Integer getPageNumber() {
		return pageSize == 0 ? 0 :
		 offset / pageSize;
	}
	
	@Override
	public int hashCode() {
		int result = 17;
		result = 37 * result 
		+ (queryId == null ? 0 : queryId)
		+ (offset == null ? 0 : getPageNumber())
		+ (pageSize == null ? 0 : pageSize);
		
		return result;
	}
}
